package edu.hwadee.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;


import edu.hwadee.entity.Carsafety;

/**
* @author luhaojian
* @version ����ʱ�䣺2018��8��9�� ����3:47:10
* ���ܣ�
*/
public class CarsafetyAction extends BasicAction implements ModelDriven<Carsafety>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Carsafety carsafety=new Carsafety();
	private Integer option;
	private String optionvalue;
	private Integer operator;
	private Integer carId;
	

	public CarsafetyAction() {
		
	}
	

	public String addCarsafety(){
		
	if(carsafety!=null) {
			if(carId!=null)
			{
				carsafety.setCar(getCarServiceImpl().search(carId));
			}
			if(operator!=null) {
				carsafety.setStaff(getStaffServiceImpl().searchById(operator));
			}
			
		}
		
		if(getCarsafetyServiceImpl().add(carsafety)){
			System.out.println("插入成功");
			return "success";
		}
		else {
			return "error";
		}
	}
	
	public String searchCarsafety(){
		System.out.println(option);
		if(option == 1) {
			carsafety.setCarsafetyId(Integer.parseInt(optionvalue));
		}
		else if(option == 2) {
			carsafety.setCar(getCarServiceImpl().search(Integer.parseInt(optionvalue)));
		
		}
		else if(option==3){
			carsafety.setStaff(getStaffServiceImpl().searchById(Integer.parseInt(optionvalue)));
		}
		else if(option==4){
			carsafety.setSafetyType(optionvalue);
		}
		
		List<Carsafety> carsafetyList =getCarsafetyServiceImpl().searchList(carsafety);
		ServletActionContext.getRequest().getSession().setAttribute("carsafetyList", carsafetyList);
		ServletActionContext.getRequest().getSession().setAttribute("dooptionS", true);

		if(option == 1) {
			carsafety.setCarsafetyId(null);
		}
		else if(option == 2) {
			carsafety.setCar(null);
		}
		else if(option == 3){
			carsafety.setStaff(null);
		}
		else if(option==4){
			carsafety.setSafetyType(null);
		}
		return "success";
	}
	
	public String updateCarsafety(){
		if(carsafety!=null) {
			if(carId!=null)
			{
				carsafety.setCar(getCarServiceImpl().search(carId));
			}
			if(operator!=null) {
				carsafety.setStaff(getStaffServiceImpl().searchById(operator));
			}
			
		}
		if(getCarsafetyServiceImpl().update(carsafety))
			return "success";
		else {
			return "error";
		}
	}
	
	public String deleteCarsafety(){
		System.out.println("deleteCarsafety");
		if(getCarsafetyServiceImpl().delete(carsafety)){
			System.out.println("删除carsafety成功");
			return "success";
		}
		else {
			return "error";
		}
	}
	
	public String showAll(){
		System.out.println( getCarServiceImpl());
		List<Carsafety> list=(List<Carsafety>) getCarsafetyServiceImpl().getList();
		ServletActionContext.getRequest().getSession().setAttribute("carsafetyList", list);
			return "success";
		
	}

	
	@Override
	public Carsafety getModel() {
		
		if(carsafety==null)
			carsafety=new Carsafety();
		return carsafety;
	}
	
	public Carsafety getCarsafety() {
		return carsafety;
	}

	public void setCarsafety(Carsafety carsafety) {
		this.carsafety = carsafety;
	}


	public Integer getOption() {
		return option;
	}


	public void setOption(Integer option) {
		this.option = option;
	}


	public String getOptionvalue() {
		return optionvalue;
	}


	public void setOptionvalue(String optionvalue) {
		this.optionvalue = optionvalue;
	}


	public Integer getCarId() {
		return carId;
	}


	public void setCarId(Integer carId) {
		this.carId = carId;
	}


	public Integer getOperator() {
		return operator;
	}


	public void setOperator(Integer operator) {
		this.operator = operator;
	}

}
